<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>数据库操作</title>
    <style>
        h1{ color:red;}
        table{
            border: 3px double red;
            border-spacing: 0;
        }
        th,td{
            border-right: 1px dotted red;
            border-bottom: 1px dotted red;
            padding: 6px 10px;
        }
        tr>:first-child{
            text-align: center;
        }
        .sql{
            color:blue;
            font-weight: bold;
            line-height: 2;
        }
    </style>
</head>
<body>
<h1>命令执行结果</h1>
<?php
$sqls=$_POST['sql'];
$sqlArray=preg_split('@\s*;\s*@',$sqls,flags:PREG_SPLIT_NO_EMPTY);


$db=new PDO('mysql:host=localhost;dbname=db','root','12qwas');
$ps=$db->query($sqls);
$sqlIndex=0;
do{
    echo "<div class='sql'>", $sqlArray[$sqlIndex++],"</div>";
    if($cc=$ps->columnCount()){ //查询操作
        echo "<table><tr><th>序号</th>";
        for($i=0;$i<$cc;$i++){
            echo "<th>", ($ps->getColumnMeta($i))['name'],"</th>";
        }
        echo "</tr>";
        $index=0;
        foreach($ps as $row){
            echo "<tr><td>",++$index,"</td>";
            for($i=0;$i<$cc;$i++){
                echo "<td>",$row[$i]??$row[($ps->getColumnMeta($i))['name']]??'NULL',"</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    }else{
        echo "<div class='result'>受影响的行数：", $ps->rowCount(),"</div>";
    }
}while($ps->nextRowset());
?>



